<template>
  <div class="login-container" v-loading.fullscreen.lock="fullscreenLoading">
    <el-tabs v-model="activeName" class="login-border-card">

      <el-tab-pane label="账号" name="first">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="用户名/手机号码"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
          <el-button class="login-button" :loading="loading" style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
<!--        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form" autocomplete="on" label-position="left">-->

<!--          <el-form-item prop="username">-->
<!--            <span class="svg-container">-->
<!--              <svg-icon icon-class="user" />-->
<!--            </span>-->
<!--            <el-input-->
<!--              ref="username"-->
<!--              v-model="registerForm.username"-->
<!--              placeholder="用户名"-->
<!--              name="username"-->
<!--              type="text"-->
<!--              tabindex="1"-->
<!--              autocomplete="on"-->
<!--            />-->
<!--          </el-form-item>-->

<!--          <el-tooltip v-model="capsTooltipRegister" content="Caps lock is On" placement="right" manual>-->
<!--            <el-form-item prop="password">-->
<!--              <span class="svg-container">-->
<!--                <svg-icon icon-class="password" />-->
<!--              </span>-->
<!--              <el-input-->
<!--                :key="passwordTypeRegister"-->
<!--                ref="password"-->
<!--                v-model="registerForm.password"-->
<!--                :type="passwordTypeRegister"-->
<!--                placeholder="用户密码"-->
<!--                name="password"-->
<!--                tabindex="2"-->
<!--                autocomplete="on"-->
<!--                @blur="capsTooltipRegister = false"-->
<!--                @keyup.enter.native="handleRegister"-->
<!--              />-->
<!--              <span class="show-pwd" @click="showPwdRegister">-->
<!--                <svg-icon :icon-class="passwordTypeRegister === 'password' ? 'eye' : 'eye-open'" />-->
<!--              </span>-->
<!--            </el-form-item>-->
<!--          </el-tooltip>-->

<!--          <el-tooltip v-model="capsTooltipRegisterQueryPassword" content="Caps lock is On" placement="right" manual>-->
<!--            <el-form-item prop="confirmPassword">-->
<!--              <span class="svg-container">-->
<!--                <svg-icon icon-class="password" />-->
<!--              </span>-->
<!--              <el-input-->
<!--                :key="passwordTypeRegisterQueryPassword"-->
<!--                ref="confirmPassword"-->
<!--                v-model="registerForm.confirmPassword"-->
<!--                :type="passwordTypeRegisterQueryPassword"-->
<!--                placeholder="确认密码"-->
<!--                name="password"-->
<!--                tabindex="2"-->
<!--                autocomplete="on"-->
<!--                @blur="capsTooltipRegisterQueryPassword = false"-->
<!--                @keyup.enter.native="handleRegister"-->
<!--              />-->
<!--              <span class="show-pwd" @click="showPwdRegisterQueryPassword">-->
<!--                <svg-icon :icon-class="passwordTypeRegisterQueryPassword === 'password' ? 'eye' : 'eye-open'" />-->
<!--              </span>-->
<!--            </el-form-item>-->
<!--          </el-tooltip>-->

<!--          <el-button class="login-button" :loading="loading" style="width:100%;" @click.native.prevent="handleRegister">注册</el-button>-->
<!--        </el-form>-->
      </el-tab-pane>

      <el-tab-pane label="扫码" name="second">
        <div style="height: 300px;"></div>
      </el-tab-pane>

      <div style="height: 50px;background-color: rgb(250,250,250)">
        <div>
          <div class="QQWeiXinButton" @click="giteeButton()">
            <img style="height: 20px;width: 20px;vertical-align: middle;" :src="require('@/assets/login/gitee.png')">
            gitee
          </div>
          <div class="QQWeiXinButton">
            <img style="height: 20px;width: 20px;vertical-align: middle" :src="require('@/assets/login/weixin.png')">
            微信
          </div>
          <div class="RegisterButton" @click="registerButton()">立即注册</div>
        </div>
      </div>
    </el-tabs>

    <div class="footer">
      <ul>
        <li>
          <a>关于我们</a>
        </li>
        <li>|</li>
        <li>
          <a>联系我们</a>
        </li>
        <li>|</li>
        <li>
          <a>人次招聘</a>
        </li>
        <li>|</li>
        <li>
          <a>商家入驻</a>
        </li>
        <li>|</li>
        <li>
          <a>广告服务</a>
        </li>
        <li>|</li>
        <li>
          <a>MaxLen 商城</a>
        </li>
        <li>|</li>
        <li>
          <a>友情链接</a>
        </li>
        <li>|</li>
        <li>
          <a>销售联盟</a>
        </li>
        <li>|</li>
        <li>
          <a>MaxLen 商城社区</a>
        </li>
        <li>|</li>
        <li>
          <a>MaxLen 商城公益</a>
        </li>
        <li>|</li>
        <li>
          <a>English Site</a>
        </li>
      </ul>
    </div>
    <p class="bq">Copyright©2004-2016 MaxLen.com 版权所有</p>
    <br /><br />

  </div>
</template>

<script>

import { registerConsumer } from '@/api/reception'
import {getToken} from "@/utils/login-token";

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 3 || value.length > 10) {
        callback(new Error('用户名不能小于 3 个字或大于 10 个字'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于 6 位'))
      } else {
        callback()
      }
    }
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('密码不一致'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      registerRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: validateConfirmPassword }]
      },
      passwordType: 'password',
      passwordTypeRegister: 'password',
      passwordTypeRegisterQueryPassword: 'password',
      capsTooltip: false,
      capsTooltipRegister: false,
      capsTooltipRegisterQueryPassword: false,
      activeName: 'first',
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      fullscreenLoading: false
    }
  },
  watch: {
    $route: {
      immediate: true,
      handler: function(route) {
        // const path = route.query.navbarKey
        // if(path !== undefined){
        //   this.redirect = path
        // }
        if(getToken() != null){
          this.$router.push({path: this.redirect !== undefined ? this.redirect : '/'})
        }

        if(route.query.code !== undefined){
          this.fullscreenLoading = true
          this.$store.dispatch('member/giteeLogin', {'code':route.query.code})
            .then(() => {
              this.fullscreenLoading = false
              this.$router.push({path: this.redirect !== undefined ? this.redirect : '/'})
            })
            .catch(() => {
              this.fullscreenLoading = false
            })
        }
      }
    }
  },
  created() {
    if(this.$route.query.username !== undefined){
      this.activeName = 'second'
      this.loginForm.username = this.$route.query.username
    }
  },
  methods: {
    registerButton() {
      let routeData = this.$router.resolve({
        path: "/register"
      });

      //必要操作，否则不会打开新页面
      window.open(routeData.href, '_blank');
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          registerConsumer(this.registerForm)
            .then(() => {
              this.$message({
                message: '注册成功',
                type: 'success'
              })
              this.activeName = 'first'
              this.loginForm.username = this.registerForm.username
              this.registerRefresh()
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    registerRefresh() {
      this.registerForm = {}
    },
    checkCapslock(e) {
      const {key} = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showPwdRegister() {
      if (this.passwordTypeRegister === 'password') {
        this.passwordTypeRegister = ''
      } else {
        this.passwordTypeRegister = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showPwdRegisterQueryPassword() {
      if (this.passwordTypeRegisterQueryPassword === 'password') {
        this.passwordTypeRegisterQueryPassword = ''
      } else {
        this.passwordTypeRegisterQueryPassword = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.fullscreenLoading = true
          this.$store.dispatch('member/login', this.loginForm)
            .then(() => {
              this.fullscreenLoading = false
              this.$router.push({path: this.redirect || '/'})
            })
            .catch(() => {
              this.fullscreenLoading = false
            })
        } else {
          return false
        }
      })
    },
    giteeButton() {
      window.location.href = 'https://gitee.com/oauth/authorize?client_id=f5b50c234cc92628d36d19985c01920921dcabbfd5ada902a9edf01913129568&redirect_uri=http%3A%2F%2Flocalhost%3A9527%2Flogin&response_type=code'
    }
  }
}
</script>

<style lang="scss">

.el-form-item__content{
  height: 60px;
}

.el-form-item.is-error .el-input__inner, .el-form-item.is-error .el-input__inner:focus, .el-form-item.is-error .el-textarea__inner, .el-form-item.is-error .el-textarea__inner:focus{
  height: 60px;
}

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;


@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 60px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000000;
      height: 60px;
      caret-color: #000000;

      &:-webkit-autofill {
        //box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
}

.el-tabs__nav-wrap::after {
  background-color: rgba(0,0,0,0);
}

.el-tabs__nav {
  margin-left: 25px;
  margin-top: 20px;
}

.el-tabs__active-bar {
  background-color: rgb(60,133,39);
  width: 40px;
}
.el-tabs__item is-top is-active{
  height: 45px;
  width: 50px;
  padding: 0px 10px;
  color: rgb(60,133,39);
  font-weight: bold;
  font-family: "微软雅黑 Light";
  font-size: 20px;
  text-align: center;
}
.el-tabs__item {
  height: 45px;
  width: 50px;
  padding: 0px 10px;
  font-weight: bold;
  font-family: "微软雅黑 Light";
  font-size: 20px;
  text-align: center;
  color: rgb(192,192,192);

  &:hover {
    color: black;
  }
}
.el-tabs__item.is-active {
  height: 45px;
  width: 50px;
  padding: 0px 10px;
  color: rgb(60,133,39);
  font-weight: bold;
  font-family: "微软雅黑 Light";
  font-size: 20px;
  text-align: center;
}
</style>

<style lang="scss" scoped>
$bg:#283443;
$dark_gray:#889aa4;
$light_gray:#eee;

body ul,
body li,
html ul,
html li {
  list-style: none;
}

.footer ul {
  width: 1100px;
  margin: 0 auto;
  height: 31px;
  display: flex;
  align-items: center;
}

.footer ul li {
  color: #999;
  margin-left: 14px;
}

.footer ul li a {
  color: #999;
  font-size: 12px;
}

.footer ul li a:hover {
  border-bottom: 1px solid #999999;
}

.bq {
  text-align: center;
  font-size: 12px;
  color: #999;
}

.QQWeiXinButton{
  height: 50px;
  line-height: 50px;
  display: inline-block;
  margin-left: 25px;

  &:hover{
    color: rgb(60,133,39);
    cursor: pointer;
  }
}

.RegisterButton{
  height: 50px;
  line-height: 50px;
  display: inline-block;
  float: right;
  margin-right: 25px;
  color: rgb(60,133,39);

  &:hover{
    cursor: pointer;
    text-decoration: underline;
  }
}

.el-form-item {
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.1);
  border-radius: 5px;
  color: #454545;
}

.login-button {
  border-radius: 0px;
  background-color: rgb(60,133,39);
  color: white;
  border-top: 4px solid rgb(128,176,114);
  border-bottom: 4px solid rgb(45,100,29);
  border-left: 4px solid rgb(45,100,29);
  border-right: 4px solid rgb(128,176,114);
}

.login-button:hover{
  cursor: pointer;
  background-color: rgb(49,50,49);
  border-top: 4px solid rgb(101,101,101);
  border-bottom: 4px solid rgb(32,33,32);
  border-left: 4px solid rgb(32,33,32);
  border-right: 4px solid rgb(101,101,101);
}

.login-container {
  //min-height: 100%;
  //height: 847px;
  width: 100%;
  height: 100%;
  //background: url("~@/assets/login/loginBackground.jpg") repeat-x;
  background-size:100% 100%;
  overflow: hidden;

  .login-border-card {
    width: 520px;
    margin: 230.5px auto;
    background-color: white;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 0px 25px 0;
    margin: 20px auto;
    overflow: hidden;
    background-color: rgba(255,255,255,0.9);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  //.title-container {
  //  position: relative;
  //
  //  .title {
  //    font-size: 26px;
  //    color: #000000;
  //    margin: 0px auto 20px auto;
  //    text-align: center;
  //    font-weight: bold;
  //  }
  //}

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
